#!/usr/bin/env python
# coding: utf-8
import os
import sys
from task.taskItem import *
class taskList:
    itemList = []
    currentPos = 0
    totalTaskItem = 0
    def __init__(self):
        pass
    def emptyItem(self):
        self.itemList.clear()
    def __init__(self,taskFile):
        nid = 0
        with open(taskFile, "rb") as f:
            for line in f:
                line = bytes.decode(line).strip()
                #split by .  get the title of taskitem
                if "." in line and not ':' in line:
                    titleInfo = line.split('.',1)
                    # if get the task title  create an item    a = taskItem()
                    # tasklist add title  set  currentParserNumber + 1
                    nid = nid+1
                    aTaskItem = taskItem(nid)
                    aTaskItem.taskItemName = titleInfo[1]
                    self.addItem(aTaskItem)
                # if not get the task title then  paser split by :
                if ":" in line:
                    propInfo = line.split(":",1)
                    if propInfo[0].strip() == "之前界面":
                        self.itemList[self.currentPos-1].currentScreen = propInfo[1]
                    if propInfo[0].strip() == "期待界面":
                        self.itemList[self.currentPos - 1].exceptScreen = propInfo[1]
                    if propInfo[0].strip() == "事前计算":
                        self.itemList[self.currentPos - 1].beforeCalcCommond = propInfo[1]
                    if propInfo[0].strip() == "事前结果":
                        self.itemList[self.currentPos - 1].beforeCalcResult = propInfo[1]
                    if propInfo[0].strip() == "事后计算":
                        self.itemList[self.currentPos - 1].afterCalcCommond = propInfo[1]
                    if propInfo[0].strip() == "事后结果":
                        self.itemList[self.currentPos - 1].afterCalcResult = propInfo[1]
                    if propInfo[0].strip() == "动作执行":
                        self.itemList[self.currentPos - 1].actionCommand = propInfo[1]
                    if propInfo[0].strip() == "出错状态转移码":
                        self.itemList[self.currentPos - 1].errorStatusNo = propInfo[1]

        self.totalTaskItem = self.currentPos
        print(self.totalTaskItem)
        self.currentPos = 0

        for i in range(self.currentPos,self.totalTaskItem):
            itemCommonPath ="./shouji/"+getHandsetType()+"/taskCommon/"+self.itemList[i].taskItemName+"/config.ini"
            if os.path.exists(itemCommonPath):
                with open(itemCommonPath, "rb") as f:
                    for line in f:
                        line = bytes.decode(line).strip()
                        if ":" in line:
                            propInfo = line.split(":", 1)
                            if propInfo[0].strip() == "期待界面" and self.itemList[i].exceptScreen =="":
                                self.itemList[i].exceptScreen = propInfo[1]
                            if propInfo[0].strip() == "之前界面" and self.itemList[i].currentScreen == "":
                                self.itemList[i].currentScreen = propInfo[1]
                            if propInfo[0].strip() == "事前计算" and self.itemList[i].beforeCalcCommond=="":
                                self.itemList[i].beforeCalcCommond = propInfo[1]
                            if propInfo[0].strip() == "事前结果" and self.itemList[i].beforeCalcResult == "":
                                self.itemList[i].beforeCalcResult = propInfo[1]
                            if propInfo[0].strip() == "事后计算" and self.itemList[i].afterCalcCommond=="":
                                self.itemList[i].afterCalcCommond = propInfo[1]
                            if propInfo[0].strip() == "事后结果" and self.itemList[i].afterCalcResult == "":
                                self.itemList[i].afterCalcResult = propInfo[1]
                            if propInfo[0].strip() == "动作执行" and self.itemList[i].actionCommand =="":
                                self.itemList[i].actionCommand = propInfo[1]
                            if propInfo[0].strip() == "出错状态转移码" and self.itemList[i].errorStatusNo == "":
                                self.itemList[i].errorStatusNo = propInfo[1]
        if 1:
             print("print the taskinfo"+taskFile+"****************************")
             for i in range(self.currentPos,self.totalTaskItem):
                print(i+1)
                print(self.itemList[i].taskItemName)
                print("之前界面:"+self.itemList[i].currentScreen)
                print("期待界面:"+self.itemList[i].exceptScreen)
                print("事前计算:"+self.itemList[i].beforeCalcCommond)
                print("事前结果:"+self.itemList[i].beforeCalcResult)
                print("动作执行:"+self.itemList[i].actionCommand)
                print("事后计算:"+self.itemList[i].afterCalcCommond)
                print("事后结果:"+self.itemList[i].afterCalcResult)
                print("出错状态转移码:"+self.itemList[i].errorStatusNo)
             print("print the taskinfo"+taskFile+"****************************")

#test task run now


    def  addItem(self,aTaskItem):
        self.itemList.append(aTaskItem)
        self.currentPos = self.currentPos+1
        pass

# a = taskItem()
# a.CalcCommond ="abccalc command"
# alist = taskList()
# alist.addItem(a)
# print(alist)